package com.hcbd.tdwt.ui.fuda.setting.activity;

import android.annotation.SuppressLint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hcbd.tdwt.R;
import com.hcbd.tdwt.ui.fuda.common.dialog.AddChannelDialog;

import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.base.Constant;
import com.hcbd.tdwt.base.GlobalParams;
import com.hcbd.tdwt.util.SharedPreferencesUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_channel_set_fuda)
public class ChannelSetActivity extends BaseActivity {
    @ViewById
    LinearLayout optionsLL;
    @ViewById
    Button configBtn;
    SharedPreferencesUtil sp;
    private String delimiter=",";
    private List<String>listStr;
    private String currentNumber;
    private String currentContent;
    private AddChannelDialog addChannelDialog;
    private final String defaultStr=Constant.PLATFORM_3;
    @AfterViews
    protected void init() {
        sp=new SharedPreferencesUtil(this);
        currentNumber=sp.getAttribute(Constant.PLATFORM_CENTER_NUMBER);

        initChannel();
        addChannelDialog=new AddChannelDialog(this, "新增平台通道", (dialog, content) -> {
            if("".equals(content)){
                toast("请输入平台号码！");
                return;
            }
            if(listStr.contains(content)){
                toast("号码不能重复！");
                return;
            }
            currentContent=sp.getAttribute(Constant.CUSTOM_CHANNEL);
            if("".equals(currentContent)){
                currentContent=content;
            }else{
                currentContent=currentContent+delimiter+content;
            }
            sp.addAttribute(Constant.CUSTOM_CHANNEL,currentContent);
            initChannel();
            hideInputKeyBord();
            dialog.dismiss();
        });
    }


    @Click
    void headLeft(){
        finish();
    }
    public void initChannel(){
        currentContent=sp.getAttribute(Constant.CUSTOM_CHANNEL);
        Log.e("currentContent1:",currentContent);
        optionsLL.removeAllViews();
        if(currentContent.equals("")){
            currentContent=defaultStr;
        }else{
            currentContent=defaultStr+delimiter+currentContent;
        }
        Log.e("currentContent2:",currentContent);
        String []str=currentContent.split(delimiter);
        listStr=new ArrayList(Arrays.asList(str));
        for(String str1:listStr){
            View view= LayoutInflater.from(this).inflate(R.layout.channel_number_item, null, false);
            TextView channelTv=view.findViewById(R.id.channelTv);
            ImageView selectImg=view.findViewById(R.id.selectImg);
            view.setOnClickListener(view1 -> {
                selectImg.setVisibility(View.VISIBLE);
                currentNumber=str1;
                initChannel();
            });
            if(defaultStr.equals(str1)){
                channelTv.setText("北斗三号通道1");
            }else {
                channelTv.setText(str1);
            }
            optionsLL.addView(view);
            if(currentNumber.equals(str1)){
                selectImg.setVisibility(View.VISIBLE);
            }else{
                selectImg.setVisibility(View.INVISIBLE);
            }
        }
    }
    @Click
    void addChannelLL(){
        AddChannelDialog.show(addChannelDialog);
    }
    @Click
    void configBtn(){
        GlobalParams.rdCentreNumber=currentNumber;
        sp.addAttribute(Constant.PLATFORM_CENTER_NUMBER,currentNumber);
        toast("配置成功");
        Log.e("rdCentreNumber:",""+GlobalParams.rdCentreNumber);
    }
}
